<?php
namespace Core\CoreCommons;

/**
 * Mail class definition. This class manage the mail with their template used.
 *
 * @package CoreCommons
 * @author  Didelot Guillaume
 * 
 * @method boolean Send($to = null)
 * @method void ParseTemplate($path)
 */
class Mail 
{
	/**
	* @property string $To The receiver address
	*/
	public $To;
	
	/**
	* @property string $Subject The subject
	*/
	public $Subject;
	
	/**
	* @property string $Message The message
	*/
	public $Message;
	
	/**
	* @property string $Text The message text
	*/
	public $Text;
	
	/**
	* @property string $Headers The headers
	*/
	private $Headers;
	
	/**
	* @property string $Type The mail type
	*/
	private $Type;

	/**
	* @property datetime $CreationDate The creation date of the mail
	*/
	private $CreationDate;
	
	/**
	* Default constructor
	*	
	* @param string $type Set the mail type 
	*/
	public function __construct($type, $values) 
	{
		Logger::Info("Construct mail $type");
		
		if($type == null || $type == '')
		{
			throw new \Exception("The -type- parameter is not set.");
		}
		
		$this->Type = $type;
		$this->CreationDate = date("Y-m-d H:i:s");
		
		$this->Headers .= "MIME-Version: 1.0 \n";
		$this->Headers .= "Content-Type: text/html; charset=iso-8859-1 \n";
		$this->Headers .= 'From: "Webmaster de cdf-91680.fr" <webmaster@cdf-91680.fr>';
		
		$filePath = sprintf("%s/Mails/%s.xml", COMMONS_DIR, $this->Type);
		
		if(file_exists($filePath) == false)
		{
			throw new \Exception("The type " . $this->Type . " is not managed : $filePath.");
		}
		
		$this->ParseTemplate($filePath, $values);
	}
	
	/**
	* This method send the built mail
	*	
	* @param mixed (string / array of sting) $to Set the receiver 
	*
	* @return boolean true if sent
	*/
	public function Send($to = null)
	{
		$result = false;
		try
		{
			if($this->Subject == null || $this->Subject == '')
			{
				throw new \Exception("The -Subject- parameter is not set.");
			}
			
			if($this->Message == null || $this->Message == '')
			{
				throw new \Exception("The -Message- parameter is not set.");
			}

			if(isset($to))
			{
				Logger::Info($this->Message);
				
				if(is_array($to) == false)
				{
					Logger::Info("Send mail to : $to / subject : $this->Subject");
					$result = mail($to, $this->Subject, $this->Message, $this->Headers);
				}
				else if(is_array($to))
				{
					foreach($to as $mail)
					{
						Logger::Info("Send mail to : $mail / subject : $this->Subject");
						$result = mail($mail, $this->Subject, $this->Message, $this->Headers);
					}
				}
				else
				{
					throw new \Exception("The argument 'to' is unknown type.");
				}
			}
			else
			{
				throw new \Exception("The argument 'to' have to be filled.");
			}
			
			if($result == false)
			{
				Logger::Error("Send mail subject : $this->Subject failed");
			}
			else
			{
				Logger::Info("Send mail subject : $this->Subject");
			}
		}
		catch (\Exception $ex)
		{
			Logger::Error($ex->getMessage());
		}
		
		return $result;
	}
	
	/*
	* This method parse xml template mail and build a Mail object
	*		
	* @param string $path Set the path of the mail template
	*	
	* @return void Set the path of the mail template 
	*/
	private function ParseTemplate($path, $values)
	{
		try
		{
			Logger::Debug("ParseTemplate $path");
			
			$mail = simplexml_load_file($path);
			$this->Subject = $mail->subject;
			$this->Message = $mail->message->asXML();

			if(isset($values))
			{
				$keys = array_keys($values);
				for ($i = 0; $i < count($values); $i++) {
					$key = $keys[$i];	
					$value = (string)$values[$key];
					Logger::Info("ParseTemplate - Replace $key with $value");
					$this->Message = str_replace($key, $value, $this->Message);
				}
			}
			$this->Message = utf8_encode($this->Message);
			Logger::Info("ParseTemplate create Message : " . $this->Message . " and subject : " . $this->Subject);
		}
		catch(\Exception $ex)
		{
			Logger::Error($ex->getMessage());
		}
	}
}
?>
